Skip to content

Conversation

@kirre-bylund
Copy link
Contributor

Add LootLocker Lifecycle Manager and Presence System

📋 Summary

This PR introduces a comprehensive service lifecycle management system and WebSocket-based presence functionality to the LootLocker Unity SDK. The changes establish a robust foundation for service coordination while adding real-time presence capabilities with platform-specific optimizations.

🎯 Key Features

LootLocker Lifecycle Manager

  • Centralized Service Management: New LootLockerLifecycleManager coordinates all SDK services with proper initialization order and dependency management
  • Service Architecture: Introduction of ILootLockerService interface for consistent service lifecycle patterns
  • Thread-Safe Operations: All service access is thread-safe with proper locking mechanisms
  • Unity Integration: Seamless Unity lifecycle event coordination (OnApplicationPause, OnApplicationFocus, OnApplicationQuit)

Event System

  • Centralized Event Management: New LootLockerEventSystem provides typed, thread-safe event handling across the SDK
  • Session Lifecycle Events: Comprehensive events for session started, refreshed, ended, expired, and local session changes
  • Memory Safety: Weak reference-based event subscriptions prevent memory leaks
  • Developer Experience: Strongly-typed event handlers with compile-time safety

Presence System

  • Real-Time WebSocket Connectivity: LootLockerPresenceClient and LootLockerPresenceManager for real-time presence features
  • Platform Support: Configurable platform enablement with recommended presets (Desktop + Console by default)
  • Battery Optimization: Mobile-specific optimizations including configurable ping intervals and automatic pause/resume handling
  • Connection Management: Automatic reconnection, latency tracking, and connection statistics
  • Multi-Session Support: Concurrent presence connections for multiple player sessions

🔧 Technical Improvements

Performance Optimizations

  • Cached Service References: RateLimiter reference cached in HTTPClient to eliminate service lookup overhead on every HTTP request
  • Efficient Event Processing: Minimal allocation event system with automatic dead reference cleanup
  • Connection Pooling: Proper WebSocket connection management with resource cleanup

Architecture Enhancements

  • Service Dependencies: Clear initialization order (RateLimiter → HTTPClient → EventSystem → PresenceManager)
  • Error Handling: Comprehensive error handling with actionable error messages throughout the stack
  • State Management: Event-driven state management replacing direct state manipulation
  • Resource Management: Proper cleanup and disposal patterns for all managed resources

Developer Experience

  • Configuration UI: Enhanced Project Settings with presence configuration options
  • Platform Detection: Automatic platform detection with sensible defaults
  • Debugging Support: Comprehensive logging and connection statistics for troubleshooting
  • Backward Compatibility: All existing APIs remain unchanged; new features are additive

🚀 API Changes

New Public APIs

// Presence System
LootLockerSDKManager.StartPresence(onConnectionStateChanged, onMessageReceived);
LootLockerSDKManager.UpdatePresenceStatus("online", metadata);
LootLockerSDKManager.GetPresenceConnectionState();
LootLockerSDKManager.IsPresenceConnected();

// Event System  
LootLockerEventSystem.Subscribe<LootLockerSessionStartedEventData>(eventType, handler);
LootLockerEventSystem.TriggerEvent(eventData);

// SDK Management
LootLockerSDKManager.ResetSDK(); // New coordinated reset method

Enhanced APIs

  • All authentication methods now trigger session lifecycle events
  • Improved error handling across all HTTP operations
  • Enhanced service initialization with proper dependency management

🔧 Breaking Changes

None - This PR is designed to be fully backward compatible. All existing APIs continue to work exactly as before.

🧪 Testing Considerations

Service Lifecycle

  • Services initialize in correct dependency order
  • Service reset functionality works correctly
  • Application lifecycle events are properly handled
  • Memory leaks prevented through proper cleanup

Presence System

  • WebSocket connections establish successfully
  • Automatic reconnection works on connection failures
  • Mobile battery optimizations function correctly
  • Multi-session presence connections work independently

Event System

  • Session events fire correctly for all authentication flows
  • Event subscriptions and unsubscriptions work properly
  • Weak references prevent memory leaks
  • Thread-safe event processing

📱 Platform Support

Presence Platform Matrix

Platform Default Notes
Windows/Mac/Linux ✅ Enabled Full WebSocket support
PlayStation/Xbox/Switch ✅ Enabled Console WebSocket support
iOS/Android ⚠️ Disabled Battery considerations; can be enabled
WebGL ⚠️ Disabled Browser WebSocket limitations
Unity Editor ✅ Enabled Development and testing

Configuration

Presence can be configured in Project Settings > LootLocker SDK > Presence Settings with platform-specific toggles and battery optimization options.

📋 Commit Structure

This PR includes 7 atomic commits for easier review:

  1. LootLockerLifecycleManager - Core service management foundation
  2. RateLimiter Service Conversion - Service architecture implementation
  3. HTTPClient Integration - Performance optimizations and service integration
  4. Event System - Centralized event management
  5. Presence System - WebSocket presence functionality
  6. SDK Manager Integration - API integration and authentication flow updates
  7. Editor Integration - Editor extensions and cleanup

🔍 Review Notes

  • Each commit can be reviewed independently
  • Service architecture follows established Unity patterns
  • All new code includes comprehensive documentation
  • Performance improvements are measurable (cached service references)
  • Memory safety is prioritized throughout (weak references, proper disposal)

🚀 Migration Guide

For Existing Users

No migration required - all existing code continues to work unchanged.

For New Presence Features

// Enable presence in Project Settings, then:
LootLockerSDKManager.StartPresence(
    onConnectionStateChanged: (playerUlid, state, error) => {
        Debug.Log($"Presence state: {state}");
    },
    onMessageReceived: (playerUlid, message, type) => {
        Debug.Log($"Presence message: {message}");
    }
);

For Event System

// Subscribe to session events:
LootLockerEventSystem.Subscribe<LootLockerSessionStartedEventData>(
    LootLockerEventType.SessionStarted,
    OnSessionStarted
);

private void OnSessionStarted(LootLockerSessionStartedEventData eventData) {
    Debug.Log($"Session started for {eventData.playerData.ULID}");
}

✅ Checklist

  • All existing functionality preserved
  • Comprehensive documentation added
  • Performance optimizations implemented
  • Memory safety ensured
  • Platform compatibility maintained
  • Editor integration complete
  • Atomic commits for easy review

This PR establishes a solid foundation for future SDK enhancements while adding powerful real-time capabilities that developers have been requesting.

@kirre-bylund kirre-bylund requested a review from Copilot November 11, 2025 17:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a comprehensive service lifecycle management system and WebSocket-based presence functionality to the LootLocker Unity SDK. The changes establish centralized service coordination through a new LootLockerLifecycleManager and add real-time presence capabilities with platform-specific optimizations, including battery-aware mobile support and automatic session management.

Key Changes:

  • Introduced LootLockerLifecycleManager for centralized service initialization, dependency management, and Unity lifecycle event coordination
  • Added LootLockerEventSystem for typed, thread-safe event handling with automatic memory leak prevention via weak references
  • Implemented WebSocket-based presence system (LootLockerPresenceManager and LootLockerPresenceClient) with platform-specific configurations and battery optimizations

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
LootLockerConfig.cs Added presence platform configuration enums and helper methods for platform detection
RemoteSessionRequest.cs Converted RemoteSessionPoller to implement ILootLockerService interface with lifecycle management
LootLockerSDKManager.cs Integrated lifecycle manager initialization and added presence API methods
ProjectSettings.cs Added presence configuration UI with platform toggles and battery optimization settings
LootLockerAdminExtension.cs Updated cleanup to use lifecycle manager instead of direct HTTP client reset
LootLockerStateData.cs Added event system integration for automatic session state management
LootLockerServerApi.cs Converted to service architecture with lifecycle manager integration
LootLockerRateLimiter.cs Implemented ILootLockerService interface with proper reset functionality
LootLockerPresenceManager.cs New presence manager service for WebSocket connection coordination
LootLockerPresenceClient.cs New WebSocket client implementation with connection management and latency tracking
LootLockerLifecycleManager.cs New centralized service lifecycle coordinator with dependency management
LootLockerHTTPClient.cs Converted to service architecture with improved memory management and queue limits
LootLockerEventSystem.cs New centralized event system with weak reference-based subscriptions
LootLockerEndPoints.cs Added presence WebSocket endpoint definition

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kirre-bylund kirre-bylund marked this pull request as ready for review November 13, 2025 14:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

Runtime/Client/LootLockerPresenceManager.cs:1

  • The LootLockerPresenceManager references StartGooglePlayGamesSession event handling but the session started event trigger is missing for this authentication method. In the SDK manager diff at line 1116, StartGooglePlayGamesSession creates playerData but doesn't call LootLockerEventSystem.TriggerSessionStarted(playerData), unlike other authentication methods in the same file.
#if LOOTLOCKER_ENABLE_PRESENCE

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@JohannesLoot JohannesLoot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor things, other than that it is all approved.
Great job on this one 👍

Copy link
Contributor Author

@kirre-bylund kirre-bylund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 24 out of 24 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor Author

@kirre-bylund kirre-bylund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed only to LootLockerLifeCycleManager so far

Copy link
Contributor Author

@kirre-bylund kirre-bylund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed only to LootLockerLifeCycleManager so far

Copy link

@tigran-sargsyan-w tigran-sargsyan-w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, just dropping a quick drive-by review 👋

Overall the new presence-related changes look consistent with the rest of the SDK, and the public surface seems easy enough to follow from a user point of view.

A couple of small thoughts:

  • If it’s not already covered somewhere else, it might be nice to add a short usage example to the docs / samples next to the other player-related APIs so people can discover this more easily.
  • Same for tests: if there are any edge cases around presence state transitions / error handling, a couple of focused tests could help catch regressions later.

Other than that, I didn’t notice anything that looked blocking from my side. 👍

@kirre-bylund kirre-bylund merged commit ac1587e into dev Dec 9, 2025
30 of 32 checks passed
@kirre-bylund kirre-bylund deleted the feat/presence branch December 9, 2025 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants